package com.jie.other.jetpack

import android.os.Bundle
import android.util.Log
import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import com.jie.commonlib.base.BaseActivity
import com.jie.other.R
import kotlin.random.Random

/**
 * liveData框架学习测试页面
 */
class LiveDataActivity : BaseActivity() {

    private val mLifecycleStr : MutableLiveData<String> = MutableLiveData()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        mLifecycleStr.observe(this) {
            Log.i(TAG, "lifecycleStr: $it")
        }
        initActionBar()
        Log.i(TAG, "onCreate")
        // 不打印
        mLifecycleStr.value = "onCreate"
        // 转换数据类型
        transData()
        // 切换数据来源
        changeForm()
        // 合并数据来源
        mergeSource()
    }

    private fun initActionBar() {
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        supportActionBar?.setTitle(R.string.other_live_data)
    }

    /**
     * 将一个LiveData转换为另一种LiveData的数据类型
     */
    private fun transData() {
        val intData : MutableLiveData<Int> = MutableLiveData()
        Transformations
            .map(intData) { input -> "data：${input?.toString()?:"0"}"}
            .observe(this) {
            Log.i(TAG, "transData: $it")
        }
        intData.value = null
    }

    /**
     * 切换数据来源
     */
    private fun changeForm() {
        val formA = MutableLiveData<String>()
        val formB = MutableLiveData<String>()
        formA.value = "formA"
        formB.value = "formB"
        val result = MutableLiveData<Boolean>()
        Transformations.switchMap(result) {
            if (it) {
                formA
            } else {
                formB
            }
        }.observe(this) {
            Log.i(TAG, "result:$it")
        }
        result.value = Random.nextBoolean()
    }

    /**
     * 合并数据来源，只要任意数据来源数据发生改变，都将触发回调
     */
    private fun mergeSource() {
        val formA = MutableLiveData<String>()
        val formB = MutableLiveData<String>()
        val result = MediatorLiveData<String>()
        result.addSource(formA) {
            Log.i(TAG, "addSourceA: $it")
            result.setValue(it)
        }
        result.addSource(formB) {
            Log.i(TAG, "addSourceB: $it")
            result.setValue(it)
        }
        result.observe(this) {
            Log.i(TAG, "mergeSource: $it")
        }
        if (Random.nextBoolean()) {
            formA.setValue("formA")
        } else {
            formB.setValue("formB")
        }
    }

    override fun onStart() {
        super.onStart()
        Log.i(TAG, "onStart")
        // 打印
        mLifecycleStr.value = "onStart"
    }

    override fun onRestart() {
        super.onRestart()
        Log.i(TAG, "onRestart")
        // 不打印
        mLifecycleStr.value = "onRestart"
    }

    override fun onResume() {
        super.onResume()
        Log.i(TAG, "onResume")
        // 打印
        mLifecycleStr.value = "onResume"
    }

    override fun onPause() {
        super.onPause()
        Log.i(TAG, "onPause")
        // 打印
        mLifecycleStr.value = "onPause"
    }

    override fun onStop() {
        super.onStop()
        Log.i(TAG, "onStop")
        // 不打印
        mLifecycleStr.value = "onStop"
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.i(TAG, "onDestroy")
        // 不打印
        mLifecycleStr.value = "onDestroy"
    }

    companion object {
        private const val TAG = "LiveDataActivity"
    }

}